'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 'select2', 'switch', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.initPage(data);
        this.editHour();
        this.changeCoach();
        this.toggleFreeze();
        this.activate();
        this.stopCamp();
        this.importzData();
        this.delay();
        this.expiryTime();
        this.changeGroup();
        this.tag();
        this.changeSite();
        this.bindMain();
        this.unbindMain();
    },
    
    initPage: function(data){
    	$('select[name="site"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
          });

          $('select[name="site"]').change(function(){ 

            var site = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/user-source?status="+data.status+"&site=" + site;

          })
    },
    
    editHour: function(status) {
        $(document).on('shown.bs.modal', '#edit-hours', function(e){

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
    
    changeCoach: function(status) {
        $(document).on('shown.bs.modal', '#arrange-coach, #change-coach', function(e){

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
      
      toggleFreeze: function() {
          $(document).on('shown.bs.modal', '#toggle-freeze', function(e){

        	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
        },
        
    activate: function() {
        $(document).on('shown.bs.modal', '#round-activate', function(e){
          $(e.currentTarget).find("form").shiftform()
        })
      },
        
      stopCamp: function() {
          $(document).on('shown.bs.modal', '#stop-camp', function(e){

            $(e.currentTarget).find("form").shiftform()
          })
        },
        
        importzData: function() {
          $(document).on('shown.bs.modal', '#import', function(e){

          })
        },
      
      delay: function() {
          $(document).on('shown.bs.modal', ' #round-delay', function(e){

              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
        },
        
       expiryTime: function() {
            $(document).on('shown.bs.modal', ' #round-expiry-time', function(e){

                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })
            })
          },
          
     changeGroup: function(status) {
          $(document).on('shown.bs.modal', '#change-group, #edit-group', function(e){
        	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
      },
      
      changeSite: function() {
          $(document).on('shown.bs.modal', '#round-change-site', function(e){
              $('select[name="siteId"]').on('change', function() {
                  var siteId = $(this).val();
                  
                  $.ajax({
                      type: 'POST',
                      url: window.__CTX__.root + '/principals/coaches?siteId=' + siteId,
                      success: function(res) {
                          var coachSelect = '<select name="coachId" class="form-control select2" data-validate="required">'
                                           + '<option></option>';
                          res = JSON.parse(res);
                          for(var index in res.data.coaches) {
                              coachSelect = coachSelect + '<option value="' + res.data.coaches[index].id + '">' + res.data.coaches[index].name + '</option>';
                          }
                          coachSelect = coachSelect + '</select>';
                          $('.coach-item').html(coachSelect);
                          
                          $('select[name="coachId"]').select2({
                              placeholder : '请选择教练',
                              minimumResultsForSearch : -1,
                              allowClear : true
                          });
                          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()
                      }
                  });
                  
                  
              });
              
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
        },
        
    tag: function(status) {
        $(document).on('shown.bs.modal', '#tag', function(e){

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
    },
    
    bindMain: function() {
        $(document).on('shown.bs.modal', '#bind-main', function(e){

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
    },
    
    unbindMain: function() {
        $(document).on('shown.bs.modal', '#unbind-main', function(e){
            $(e.currentTarget).find("form").shiftform();
        })
    }
  }
})
